package org.twndmv.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.twndmv.bo.Sex;
import org.twndmv.bo.State;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.PhysicalDetailsDTO;
import org.twndmv.dto.ResidenceDTO;
import org.twndmv.ejb.DmvTestUtilRemote;
import org.twndmv.ejb.DriverMgmtRemote;

public class DriverMgmtIT {
	private static Log log = LogFactory.getLog(DriverMgmtIT.class);
	private InitialContext jndi;

    private static final String dmvDriverJNDI = System.getProperty("jndi.name.driver",
            "twnDmvEAR/twnDmvEJB/DriverMgmtEJB!org.twndmv.ejb.DriverMgmtRemote"); 
    private static final String dmvTestUtilJNDI = System.getProperty("jndi.name.dmvTestUtil",
            "twnDmvEAR/twnDmvEJB/DmvTestUtilEJB!org.twndmv.ejb.DmvTestUtilRemote");
    
    private DmvTestUtilRemote testUtil;
    private DriverMgmtRemote driverManager;
    
    @Before
	public void setUp() throws Exception {
		log.debug("===== TestDmvIT.init =====");

	        jndi = new InitialContext();
	        jndi.lookup("/"); //do a quick comms check of JNDI

	       
	        testUtil = (DmvTestUtilRemote)jndi.lookup(dmvTestUtilJNDI);
	        driverManager = (DriverMgmtRemote)jndi.lookup(dmvDriverJNDI);
	        
	        assertNotNull(testUtil);
	        assertNotNull(driverManager);
	        testUtil.resetAll();
	}
    
    @Test
    public void testDriverManager() {
    	PersonDTO testPerson = new PersonDTO();
	    testPerson.setFirstName("Steve");
	    testPerson.setMiddleName("Danger");
	    testPerson.setLastName("Jobs");
	    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
	    testDetails.setHeight(64);
	    testDetails.setWeight(180);
	    testDetails.setSEX(Sex.MALE);
	    testPerson.setPhysicalDetails(testDetails);
	    
	    driverManager.addDriver(testPerson);
	    
	    List<PersonDTO> allDrivers = driverManager.getAllDrivers();
	    assertEquals(1, allDrivers.size());
	    
	    List<PersonDTO> resultPersons = driverManager.getDriversByLastName("Jobs");
	    assertNotNull(resultPersons);
	    assertEquals(1,resultPersons.size());
	    PersonDTO resultPerson = resultPersons.get(0);
	    assertNotNull(resultPerson);
	    assertTrue(resultPerson.getFirstName().equalsIgnoreCase(testPerson.getFirstName()));
	    assertTrue(resultPerson.getLastName().equalsIgnoreCase(testPerson.getLastName()));
	    assertNull(resultPerson.getCurrentResidence());
	    assertEquals(0, resultPerson.getResidences().size());
	    assertEquals(0, resultPerson.getRegistrations().size());
	    
	    ResidenceDTO testResidence = new ResidenceDTO();
	    testResidence.setStreetNo(123);
	    testResidence.setStreetName("Cool Street");
	    testResidence.setCity("Cleveland");
	    testResidence.setState(State.OH);
	    testResidence.setZipCode(45398);
	    testResidence.setMoveIn(new Date(1234456));
	    testResidence.setMoveOut(null);
	    driverManager.moveResidence(resultPerson.getId(), testResidence);
	    
	    PersonDTO peep3 = driverManager.getDriver(resultPerson.getId());
	    ResidenceDTO resultRes = peep3.getCurrentResidence();
	    assertNotNull(resultRes);
	    assertEquals(testResidence.getStreetNo(), resultRes.getStreetNo());
	    assertEquals(testResidence.getZipCode(), resultRes.getZipCode());
	    assertNull(resultRes.getMoveOut());
    }
}
